const ModelClassifyTag = require('../../models/classify')
const uuid = require('uuid');
const controlTool = require('../tool')
const tool = require('../../utils/commonTools')

const TagCategories = {
    "0": "语种",
    "1": "风格",
    "2": "场景",
    "3": "情感",
    "4": "主题"
}

// 删除标签
exports.delClassifyTag = (req, res, next) => {

    ModelClassifyTag.delClassifyTag(req.query
        , (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "删除成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "删除失败",
                    status: 200,
                    error
                })
            })
        });
}
// 编辑标签
exports.updateClassifyTag = (req, res, next) => {
    const Data = req.body;
    const { id } = Data;
    if (id) {
        delete Data.id;
        ModelClassifyTag.updateClassifyTag(Data, {
            id
        }, (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "更新成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "更新失败",
                    status: 200,
                    error
                })
            })
        });
    }
}

/** 添加标签 */
exports.addClassifyTag = (req, res, next) => {
    ModelClassifyTag.addClassifyTag({
        ...req.body,
    },
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "添加成功",
                    status: 200,
                    affectedRows: value.affectedRows
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "添加失败",
                    status: 200,
                    error
                })
            })
        });
}
/** 查找标签 */
exports.findClassifyTag = async (req, res, next) => {
    let ClassifyTag = await ModelClassifyTag.findClassifyTag(req.body);
    ClassifyTag.success((value) => {
        if (value.length) {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找到标签",
                    value: value,
                    status: 200,
                    length: value.length
                })
            })
        } else {
            res.render('fail', {
                data: JSON.stringify({
                    message: "没有找到标签",
                    value: value,
                    length: value.length
                })
            })
        }
    });

    ClassifyTag.fail((error) => {
        res.render('fail', {
            data: JSON.stringify({
                message: "查找失败",
                error
            })
        })
    })
}
//查找所有标签
exports.list = (req, res, next) => {
    ModelClassifyTag.findAll(
        (value) => {
            res.render('success', {
                data: JSON.stringify({
                    message: "查找成功",
                    status: 200,
                    value: {
                        sub: value,
                        categories: TagCategories
                    },
                    length: value.length
                })
            })
        }, (error) => {
            res.render('fail', {
                data: JSON.stringify({
                    message: "查找失败",
                    status: 200,
                    error
                })
            })
        });
}